'''
/*
 * @Author: wevsmy 
 * @Date: 2017-10-24 11:41:41 
 * @Last Modified by: wevsmy
 * @Last Modified time: 2017-10-24 14:38:55
 */
'''
from threading import Thread,Lock
import time

g_num = 0

def work1():
    global g_num
    mutex.acquire()
    for i in range(1000000):
        g_num += 1
    mutex.release()
    print("----in work1, g_num is %d---"%g_num)


def work2():
    global g_num
    mutex.acquire()
    for i in range(1000000):
        g_num += 1
    mutex.release()
    print("----in work2, g_num is %d---"%g_num)


print("---线程创建之前g_num is %d---"%g_num)

#创建一把互斥锁,默认打开
mutex = Lock()
t1 = Thread(target=work1)
t1.start()

#延时一会，保证t1线程中的事情做完
#time.sleep(1)

t2 = Thread(target=work2)
t2.start()